﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace HamsterSimulator
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool activated = true;
        private string path = string.Empty;
        public MainWindow()
        {
            InitializeComponent();
            //LoadingScreen.Focus();
            //WindowContent.Children.Remove(LoadingScreen);
        }

        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = activated;
        }

        private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                SaveFileDialog sfd = new SaveFileDialog()
                {
                    Filter = "C# Hamster Program|*.csham",
                    AddExtension = true,
                    //CreatePrompt = true,
                    OverwritePrompt = true
                };

                if (sfd.ShowDialog(this) == true)
                {
                    using (Stream s = sfd.OpenFile())
                    {
                        using (StreamWriter sw = new StreamWriter(s))
                        {/*
                            TextRange tr = new TextRange(TextEditor.Document.ContentStart, TextEditor.Document.ContentEnd);
                            sw.Write(tr.Text);*/

                            sw.Write(TextEditor.Text);
                        }
                    }
                }
            }
        }

        private void Open_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = activated;
        }

        private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog()
            {
                Filter = "C# Hamster Program|*.csham",
                CheckFileExists = true,
                CheckPathExists = true,
                DereferenceLinks = true,
                InitialDirectory = path
            };

            if (ofd.ShowDialog() == true)
            {
                using (Stream s = ofd.OpenFile())
                {
                    using (StreamReader sr = new StreamReader(s))
                    {
                        //TextEditor.Document = new FlowDocument(new Paragraph(new Run(sr.ReadToEnd())));
                        TextEditor.Text = sr.ReadToEnd();
                    }
                }
            }
            
        }
        /*
        private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
        {
            // Search immediate children first (breadth-first)
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);

                if (child != null && child is T)
                    return child as T;

                else
                {
                    T childOfChild = FindVisualChild<T>(child);

                    if (childOfChild != null)
                        return childOfChild;
                }
            }

            return null;
        } */

        private void Close_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = activated;
        }

        private void Close_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            App.Current.Shutdown();
        }
    }
}
